package cn.edu.scau.dormitory_management_system.controller;

import java.text.Collator;
import java.util.*;

import cn.edu.scau.dormitory_management_system.entity.BedEntity;
import cn.edu.scau.dormitory_management_system.entity.DormEntity;
import cn.edu.scau.dormitory_management_system.entity.DormbuildEntity;
import cn.edu.scau.dormitory_management_system.entity.StudentEntity;
import cn.edu.scau.dormitory_management_system.service.BedService;
import cn.edu.scau.dormitory_management_system.service.DormService;
import cn.edu.scau.dormitory_management_system.service.DormbuildService;
import cn.edu.scau.dormitory_management_system.service.StudentService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 *
 * @author wanglingfu
 * @email 1006340605@qq.com
 * @date 2021-08-02 14:35:10
 */
@RestController
@RequestMapping("/admin/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private BedService bedService;

    @Autowired
    private DormService dormService;

    @Autowired
    private DormbuildService dormbuildService;

    @PostMapping("/getMajor")
    public Object get(String dept) {
        JSONObject object = new JSONObject();

        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("academic", dept);
        List<String> majors = new ArrayList<>();
        for (StudentEntity record : studentService.list()) {
            if(record.getDepartment().isEmpty()) continue;
            if(!majors.contains(record.getDepartment()))
                majors.add(record.getDepartment());
        }
        if(majors.isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            majors.sort((o1,o2)->{
                Collator instance = Collator.getInstance(Locale.CHINA);
                return instance.compare(o1, o2);
            });
            object.put("majors", majors);
        }

        return object;
    }

    @PostMapping("/getDormAndBuild")
    public Object getDormAndBuild(@RequestParam("Id")String Id){
        JSONObject object = new JSONObject();

        DormEntity dormEntity = studentService.getDorm(Id);
        DormbuildEntity dormbuildEntity = studentService.getBuild(Id);

        if(dormbuildEntity == null){
            object.put("code", 0);
            object.put("msg", "无数据");
        }
        else{
            object.put("code", 200);
            object.put("dorm",dormEntity);
            object.put("dormbuild",dormbuildEntity);
        }

        return object;
    }

    @PostMapping("/getAllDept")
    public Object getDept() {
        JSONObject object = new JSONObject();

        List<String> academic = new ArrayList<>();
        for (StudentEntity record : studentService.list()) {
            if(record.getAcademic().isEmpty()) continue;
            if(!academic.contains(record.getAcademic()))
                academic.add(record.getAcademic());
        }
        if(academic.isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            academic.sort((o1,o2)->{
                Collator instance = Collator.getInstance(Locale.CHINA);
                return instance.compare(o1, o2);
            });
            object.put("academic", academic);
        }

        return object;
    }
}
